//
//  RegisterController.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "RegisterController.h"
#import "Device.h"
#import "Define.h"

@interface RegisterController ()

@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建左上角返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, KSWidth / 18, KSWidth / 18);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-unie61e"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBackBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftBackBtn;
    
    NSArray *registerArray = @[@"邮箱", @"密码", @"用户名"];
    
    NSArray *placeArray = @[@"注册邮箱", @"请输入密码", @"请输入昵称"];
    
    for (int i =0; i<3; i++) {
        //创建左侧字体
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100+i*60, 100, 30)];
        
        label.text = registerArray[i];
        
        [self.view addSubview:label];
        
        //创建右边用户注册的内容
        
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30+label.frame.size.width, 100+i*60, KSWidth/2, 30)];
        
        textField.placeholder = placeArray[i];
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        textField.borderStyle = UITextBorderStyleRoundedRect;
//        [textField.layer setCornerRadius:5];
//        
//        textField.layer.borderWidth = 2;
        
        textField.tag = 100+i*100;
        
        [self.view addSubview:textField];
    }
    
    //注册 和 取消注册 按钮
    //注册
    NSArray *buttonArray = @[@"注册", @"取消"];
    for (int i =0; i<2; i++) {
        UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        loginButton.frame = CGRectMake(KSWidth/4+i*100,KSHeight/2 , KSWidth/4, 30);
        
        loginButton.layer.borderColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0].CGColor;
        
        [loginButton.layer setCornerRadius:10];
        
        loginButton.layer.borderWidth = 1;
        
        [loginButton setTitle:buttonArray[i] forState:UIControlStateNormal];
        
        [loginButton addTarget:self action:@selector(loginButton:) forControlEvents:UIControlEventTouchUpInside];
        
        loginButton.tag = 1000+i*100;
        
        [self.view addSubview:loginButton];
    }
    
    
}

// 返回按钮触发
- (void)backBtnAction:(UIButton *)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark--注册和取消按钮触发的事件---
-(void)loginButton:(UIButton *)btn{
    //点击注册按钮
    if (btn.tag == 1000) {
        //邮箱
        UITextField *emailTextField = [self.view viewWithTag:100];
        
        //密码
        UITextField *passWordTextField = [self.view viewWithTag:200];
        
        //用户名
        UITextField *userTextField = [self.view viewWithTag:300];
        
       
        
        //先清除缓存用户的对象
        [BmobUser logout];
        
        //这是bmob自带的用户注册方式
        BmobUser *user = [[BmobUser alloc]init];
        
        [user setUsername:userTextField.text];
        
        [user setPassword:passWordTextField.text];
        
        [user setEmail:emailTextField.text];
        //判断是否成功的block方法
        [user signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                //ios7后的方法
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功 进入登陆界面" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    //注册成功后 进入登陆界面
                    [self.navigationController popViewControllerAnimated:YES];
                }];
                
                //添加方法
                [alertVc addAction:otherAction];
                
          [self presentViewController:alertVc animated:YES completion:nil];
                
                
        }
            //注册失败
            else {
                
                [self alertActionWithTitle:@"提示" message:@"注册失败 请重新注册" other:@"确定"];
                
            }

        }];
    }
    
    //点击取消按钮 返回登陆界面
    else if(btn.tag == 1100){
        
        [self.navigationController popViewControllerAnimated:NO];
    }
    
}

//提示页面
- (UIAlertController *)alertActionWithTitle:(NSString *)title message:(NSString *)message other:(NSString *)other
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:otherAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    return alertVc;
}

#pragma mark--触摸取消键盘 ---
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITextField *textField1 = [self.view viewWithTag:100];
    
    UITextField *textField2 = [self.view viewWithTag:200];
    
    UITextField *textField3 = [self.view viewWithTag:300];
    
    [textField1 resignFirstResponder];
    
    [textField2 resignFirstResponder];
    
    [textField3 resignFirstResponder];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
